<?php
/*
 * Created on 2010-10-7
 *
 * @author : falcon
 * Class :
 * Function :
 * Usage : 操作文件的类
 * http://i.amfalcon.tk/myblog/
 */
class FileAction{






/**
 *
 * 单/多个文件压缩后下载
 *
 * @param	 array|string $files 原始文件名过长会出不可预料的错误，单个文件也可以用array
 * @param	 string	$fileName 目标文件名，留空将使用随机文件名,要有.zip后缀
 * @param bool $overwrite	 是否覆盖重名文件
 * @param string $readme 额外的文件
 *
 * @return bool|null
 */
 
    function download_txt($file,$fileName=''){
        if(empty($fileName)) {$fileName=uniqid().".txt";} //未指定文件名使用随机名
        if(!file_exists($file)) return false;
        else{
        	header("Content-length: ".filesize($file));
	      header('Content-type: application/'.filetype($file));
			header("Content-Disposition: attachment; filename={$fileName}");
	      readfile($file);
			unlink($file);
        }
    }
 
	function download_zip($files,$fileName='',$overwrite=false,$readme=''){

		if(!class_exists ("ZipArchive")) die("ZipArchive类不存在，请确认是否开启zip扩展");
		if(empty($fileName)) {$fileName=uniqid().".zip";} //未指定文件名使用随机名

		if(is_array($files) && file_exists($readme)){$files[]=$readme;}
		$rs=$this->create_zip($files,$fileName,$overwrite);

		if($rs) {

			header("Content-length: ".filesize($fileName));
	      header('Content-type: application/'.filetype($fileName));
			header("Content-Disposition: attachment; filename={$fileName}");
	      readfile($fileName);
			unlink($fileName);

		}else

		return false;


	}






/**
 * 解压文件
 *
 * @parm string
 * @parm	string
 *@return bool|string
 * 用法：$this->unzip_file(bbb.zip,"c:/");
 *
 */

public function unzip_file($file, $destination){
    // create object
    $zip = new ZipArchive() ;
    // open archive
    if ($zip->open($file) !== TRUE) {
        die ("找不到压缩文件");
        return false;
    }
    // extract contents to destination directory
   	if($zip->extractTo($destination)){

    		$zip->close();
    		return true;
   	}else {
   		return false;
   	}
    //echo 'Archive extracted to directory';
}


//修改后支持多个文件或单个文件的压缩
function create_zip($files,$destination = '',$overwrite = true) {
    //if the zip file already exists and overwrite is false, return false
    if(file_exists($destination) && !$overwrite) { return false; }
    //vars
    $valid_files = array();
    //if files were passed in...
    if(is_array($files)) {
        //cycle through each file
        foreach($files as $file) {
            //make sure the file exists
            if(file_exists($file)) {
                $valid_files[] = $file;
            }
        }
    }
        if(is_string($files) && file_exists($files)) {

    		$valid_files=$files;

    	}


    //if we have good files...
    if(count($valid_files)) {
        //create the archive
        $zip = new ZipArchive();
        if($zip->open($destination,$overwrite ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE) !== true) {
            return false;
            
        }
        //add the files

        if(is_array($valid_files)){
            foreach($valid_files as $file) {
                    $file_in=basename($file);
                $zip->addFile($file,$file_in); //file_in是包里的路径，file是实际文件的路径 
            }
        }else {
        		$files_in=basename($files);
        		$zip->addFile($files,$files_in);
        }
        //debug
        //echo 'The zip archive contains ',$zip->numFiles,' files with a status of ',$zip->status;

        //close the zip -- done!
        $zip->close();

        //check to make sure the file exists
        //return "file isn't here";
        return file_exists($destination) ?$destination:false;
    }
    else
    {
        return false;
    }
}

}

?>
